package com.all.widget.time;

import java.util.Calendar;

import com.all.common.BaseDialog;
import com.all.ui.R;
import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.EditText;
import android.widget.TextView;

public class DateSetterDialog extends BaseDialog implements
		OnDateChangedListener {

	private static EditText mTimeSetter;
	private TextView mStartDateTv;
	private Button mConfirm, mCancel;
	private DateSetter mDatePicker;
	
	private int start_year;
	private int start_monthOfYear;
	private int start_dayOfMonth;
	
	private OnDateSetListener onDateSetListener;
	public static EditText getmTimeSetter() {
		return mTimeSetter;
	}

	public static void setmTimeSetter(EditText mTimeSetter) {
		DateSetterDialog.mTimeSetter = mTimeSetter;
	}

	public DateSetterDialog(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		initDialog(context);
	}

	public DateSetterDialog(Context context, boolean cancelable,
			OnCancelListener cancelListener) {
		super(context, cancelable, cancelListener);
		initDialog(context);
	}

	public DateSetterDialog(Context context, int theme) {
		super(context, theme);
		initDialog(context);
	}

	

	private void initDialog(Context context) {
		// TODO Auto-generated method stub
		setContentView(R.layout.date_setter_dialog);
		
		mStartDateTv = (TextView) findViewById(R.id.beginDate);
		mConfirm = (Button) findViewById(R.id.confirm);
		mCancel = (Button) findViewById(R.id.cancel);
		mDatePicker = (DateSetter) findViewById(R.id.startDatePicker);
		
		mConfirm.setOnClickListener(clickListener);
		mCancel.setOnClickListener(clickListener);
		
		Calendar calendar = Calendar.getInstance();
		int year = calendar.get(Calendar.YEAR);
		int monthOfYear = calendar.get(Calendar.MONTH);
		int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);

		mDatePicker.init(year, monthOfYear, dayOfMonth, this);
		
		setStartDateText(year, monthOfYear, dayOfMonth);
	}
	
	private void setStartDateText(int year, int monthOfYear, int dayOfMonth) {
		this.start_year = year;
		this.start_monthOfYear = monthOfYear;
		this.start_dayOfMonth = dayOfMonth;
		mStartDateTv.setText(start_year
				+ "-"
				+ ((start_monthOfYear + 1) >= 10 ? (start_monthOfYear + 1)
						: ("0" + (start_monthOfYear + 1)))
				+ "-"
				+ (start_dayOfMonth >= 10 ? start_dayOfMonth
						: ("0" + start_dayOfMonth)) + " "
				+ getWeekDay(start_year, start_monthOfYear, start_dayOfMonth));
	}
	
	private String getWeekDay(int year, int month, int day) {
		String[] weekdays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
		Calendar calendar = Calendar.getInstance();
		calendar.set(year, month, day);
		int weekdayInt = calendar.get(Calendar.DAY_OF_WEEK) - 1;
		if (weekdayInt < 0) {
			weekdayInt = 0;
		}
		return weekdays[weekdayInt];
	}
	
	private Button.OnClickListener clickListener = new Button.OnClickListener() {

		@Override
		public void onClick(View v) {
			if (v == mConfirm) {
				if (null == onDateSetListener) {
					onDateSetListener = new OnDateSetListener() {
					};
				}
					onDateSetListener.onDateSet(start_year, start_monthOfYear,
							start_dayOfMonth);
					dismiss();
			} else if (v == mCancel) {
				dismiss();
			}
		}
	};
	
	public abstract class OnDateSetListener {
		public void onDateSet(int start_year, int start_monthOfYear,
				int start_dayOfMonth) {
			mTimeSetter.setText(start_year
					+ "-"
					+ ((start_monthOfYear + 1) >= 10 ? (start_monthOfYear + 1)
							: ("0" + (start_monthOfYear + 1)))
					+ "-"
					+ (start_dayOfMonth >= 10 ? start_dayOfMonth
							: ("0" + start_dayOfMonth))
					);
		}
	}

	@Override
	public void onDateChanged(DatePicker view, int year, int monthOfYear,
			int dayOfMonth) {
		// TODO Auto-generated method stub
		setStartDateText(year, monthOfYear, dayOfMonth);
	}
	

}